package com.zatech.cgnci.project.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zatech.cgnci.project.base.context.UserContext;
import com.zatech.cgnci.project.base.utils.ConvertUtils;
import com.zatech.cgnci.project.dto.CgnciApprovalConfigAuthQueryDTO;
import com.zatech.cgnci.project.dto.CgnciApprovalConfigAuthUserDTO;
import com.zatech.cgnci.project.dto.CgnciApprovalConfigQueryDTO;
import com.zatech.cgnci.project.dto.CgnciApprovalConfigRoleUserDTO;
import com.zatech.cgnci.project.entity.CgnciApprovalConfigRoleUserDO;
import com.zatech.cgnci.project.entity.CgnciUserInfoDO;
import com.zatech.cgnci.project.entity.UserEntity;
import com.zatech.cgnci.project.service.CgnciApprovalConfigRoleUserService;
import com.zatech.cgnci.project.service.CgnciApprovalConfigService;
import com.zatech.cgnci.project.service.CgnciUserInfoService;
import com.zatech.cgnci.project.vo.CgnciApprovalConfigNodeVO;
import com.zatech.cgnci.project.vo.CgnciApprovalConfigPageVO;
import com.zatech.cgnci.project.vo.CgnciApprovalConfigRoleUserVO;
import com.zatech.cgnci.project.vo.CgnciApprovalConfigVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 审批流程节点规则配置角色用户表 前端控制器
 * </p>
 *
 * @author Generator
 * @since 2025-02-12
 */
@RestController
@RequestMapping("/api/approval-config")
@RequiredArgsConstructor
@Api(produces = "application/json", tags = "工作流api接口文档")
public class CgnciApprovalConfigController {

    private final CgnciApprovalConfigService approvalConfigService;

    private final CgnciApprovalConfigRoleUserService approvalConfigRoleUserService;

    @PostMapping("/page")
    @ApiOperation(value = "分页列表查询")
    public IPage<CgnciApprovalConfigPageVO> page(@RequestBody CgnciApprovalConfigQueryDTO queryDTO) {
        return approvalConfigService.pageApprovalConfig(queryDTO);
    }

    @GetMapping("/listTaskType")
    @ApiOperation(value = "获取任务类型下拉")
    public List<CgnciApprovalConfigVO> listTaskType() {
        List<CgnciApprovalConfigVO> list = approvalConfigService.listTaskType();
        Map<String, CgnciApprovalConfigVO> uniqueTaskTypeMap = list.stream()
                .collect(Collectors.toMap(CgnciApprovalConfigVO::getTaskType, config -> config, (existing, replacement) -> existing));
        return new ArrayList<>(uniqueTaskTypeMap.values());
    }

    @GetMapping("/listApprovalNode")
    @ApiOperation(value = "查询该任务的所有结点")
    public List<CgnciApprovalConfigNodeVO> listApprovalNode(@RequestParam(value = "taskType") @NotBlank(message = "taskType不能为空") String taskType) {
        return approvalConfigService.listApprovalNode(taskType);
    }

    @GetMapping("/listApprovalUser")
    @ApiOperation(value = "查询该任务该结点的审批人")
    public List<CgnciApprovalConfigRoleUserVO> listApprovalUser(@RequestParam(value = "taskType") @NotBlank(message = "taskType不能为空") String taskType,
                                                                @RequestParam(value = "currentApproverNode") @NotBlank(message = "currentApproverNode不能为空") String currentApproverNode) {
        List<CgnciApprovalConfigRoleUserDO> list = approvalConfigRoleUserService.listApprovalUser(taskType, currentApproverNode);
        return ConvertUtils.convert(list, CgnciApprovalConfigRoleUserVO.class);
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation(value = "新增或者修改审批人")
    public void saveOrUpdateApprovalUser(@RequestBody @Valid CgnciApprovalConfigRoleUserDTO roleUserDTOList) {
        approvalConfigRoleUserService.saveOrUpdateApprovalUser(roleUserDTOList);
    }

    @GetMapping("/deleteById")
    @ApiOperation(value = "删除(回收权限)")
    public void deleteById(@RequestParam(value = "roleUserId") Long roleUserId) {
        CgnciApprovalConfigRoleUserDO recycleAuthUser = approvalConfigRoleUserService.getById(roleUserId);
        approvalConfigRoleUserService.recycleAuthUser(recycleAuthUser);
    }

    @GetMapping("/deleteByIds")
    @ApiOperation(value = "批量删除(批量回收权限)")
    public void deleteByIds(@RequestParam(value = "roleUserIds") String roleUserIds) {
        List<Long> roleUserIdList = Arrays.stream(roleUserIds.split(",")).map(Long::valueOf).collect(Collectors.toList());
        roleUserIdList.forEach(roleUserId -> {
            CgnciApprovalConfigRoleUserDO recycleAuthUser = approvalConfigRoleUserService.getById(roleUserId);
            approvalConfigRoleUserService.recycleAuthUser(recycleAuthUser);
        });
    }

    /****************授权接口***********************/

    @GetMapping("/listFlowTask")
    @ApiOperation(value = "点击授权,返回流程任务下拉值")
    public List<CgnciApprovalConfigRoleUserVO> listFlowTask() {
        // 获取当前登录用户
        UserEntity user = UserContext.getUser();
        // 入口控制代理人没有授权权限
        return approvalConfigRoleUserService.listFlowTask(user.getUsername(), user.getUsercode());
    }

    @PostMapping("/saveAuthorizedUser")
    @ApiOperation(value = "确认授权")
    public void saveAuthorizedUser(@RequestBody @Valid CgnciApprovalConfigAuthUserDTO authUserDTO) {
        approvalConfigRoleUserService.saveAuthorizedUser(authUserDTO);
    }

    @PostMapping("/auth/page")
    @ApiOperation(value = "授权记录分页列表")
    public IPage<CgnciApprovalConfigRoleUserVO> authPage(@RequestBody CgnciApprovalConfigAuthQueryDTO queryDTO) {
        // 获取当前登录用户
        UserEntity user = UserContext.getUser();
        queryDTO.setAuthCode(user.getUsercode());
        queryDTO.setAuthName(user.getUsername());
        return approvalConfigRoleUserService.authPage(queryDTO);
    }

}

